package com.byd.dms.fastjson;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.BeanContext;
import com.alibaba.fastjson.serializer.ContextValueFilter;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.byd.dms.entity.Dept;
import com.byd.dms.entity.User;
import com.byd.dms.entity.User2;
import org.junit.jupiter.api.Test;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

public class FastDemo4 {
    private static final Dept deptProp;
    private static final User userProp;
    private static final User2 user2Prop;
    
    static {
        userProp = new User("刘阿勇", 18, "男");
        Dept dept2 = new Dept(5L, "测试部", 52);
        userProp.setDept(dept2);
        userProp.setHobbies(Arrays.asList("吃饭", "睡觉", "乌拉圈"));
        
        deptProp = new Dept(3L, "开发部", 38);
        User user1 = new User("秦疆1号", 3, "男");
        User user2 = new User("秦疆2号", 3, "男");
        User user3 = new User("秦疆3号", 3, "男");
        
        List<User> list = new ArrayList<User>();
        list.add(user1);
        list.add(user2);
        list.add(user3);
        
        deptProp.setUsers(list);
        
        user2Prop = new User2("韩梅梅", 22, "女");
    }
    
    @Test
    public void replace() {
        String str = "{\"map\":\"{6:{\\\"deptId\\\":6,\\\"detpCount\\\":62,\\\"detpName\\\":\\\"产品\\\"}," +
                "7:{\\\"deptId\\\":7,\\\"detpCount\\\":32,\\\"detpName\\\":\\\"运营\\\"}}\"}";
        System.out.println(str);
        String s = str.replaceAll("\\\\", "");
        System.out.println(s);
    }
    
    public static void main(String[] args) {
        
        ContextValueFilter valueFilter = new ContextValueFilter() {
            public Object process(
                    BeanContext context, Object object, String name, Object value) {
                
                System.out.println("name = " + name);
                System.out.println("value = " + value);
                if (name.equals("map") && value instanceof LinkedList) {
                    //System.out.println("value.getClass().getName() = " + value.getClass().getName());
                    LinkedList<Dept> tmp = (LinkedList) value;
                    Map<Long, Dept> maps = tmp.stream().collect(Collectors.toMap(Dept::getDeptId,
                            Function.identity()));
                    System.out.println(JSON.toJSONString(maps));
                    System.out.println(JSON.toJSONString(maps).replaceAll("\\\\", ""));
    
                    return "{0:{\"deptId\":6,\"detpCount\":62,\"detpName\":\"产品\"},1:{\"deptId\":7,\"detpCount\":32," +
                            "\"detpName\":\"运营\"}}";
                    //return JSON.toJSONString(maps).replaceAll("\\\\", "");
                }
                return null;
            }
        };
        
        Map<Integer, Dept> map = new HashMap<>();
        map.put(0, new Dept(6L, "产品", 62));
        map.put(1, new Dept(7L, "运营", 32));
        user2Prop.setMap(new LinkedList<>(map.values()));
        String jsonOutput = JSON.toJSONString(user2Prop, valueFilter);
        System.out.println("\njsonOutput = " + jsonOutput);
        
        System.out.println("user2Prop=" + JSON.toJSONString(user2Prop));
        
        Map<Integer, Dept> mapOfUser = new HashMap<>();
        mapOfUser.put(0, new Dept(6L, "产品", 62));
        mapOfUser.put(1, new Dept(7L, "运营", 32));
        userProp.setMap(mapOfUser);
        System.out.println(JSON.toJSONString(userProp));
        // 将jsonOutput 转为 User
        User user = JSON.parseObject(jsonOutput, User.class);
        System.out.println("user = " + user);
    }
    
    private static void beanToArray() {
        List<User> users = deptProp.getUsers();
        // FastJson 还支持 BeanToArray 序列化功能：
        String jsonOutput = JSON.toJSONString(users, SerializerFeature.BeanToArray);
        System.out.println("jsonOutput = " + jsonOutput);
    }
    
    // todo listToMap
    private static void listToMap() {
        user2Prop.getMap().add(new Dept(6L, "产品", 62));
        user2Prop.getMap().add(new Dept(7L, "运营", 32));
        String jsonStr = JSON.toJSONString(user2Prop);
        System.out.println("jsonStr = " + jsonStr);
        
        
        //解析时json将list转为map User2.map ===> User.map List --->  Map
        User user = JSON.parseObject(jsonStr, User.class);
        System.out.println("user = " + user);
    }
    
    // todo mapToList
    private static void mapToList() {
        Map<Integer, Dept> map = new HashMap<>();
        map.put(0, new Dept(6L, "产品", 62));
        map.put(1, new Dept(7L, "运营", 32));
        userProp.setMap(map);
        String jsonStr = JSON.toJSONString(userProp);
        System.out.println("jsonStr = " + jsonStr);
        
        //JSONObject jsonObject1 = (JSONObject) JSON.toJSON(user);
        //System.out.println("map: " + jsonObject1.getString("map"));
        
        //解析时json将map转为list User.map ===> User2.map  Map ---> List
        
        
        // 直接转换报错
        User2 user2 = JSON.parseObject(jsonStr, User2.class);
        System.out.println("user2 = " + user2);
    }
}
